import { _decorator, BoxCollider2D, Component, ERigidBody2DType, Node, RigidBody2D, Size, tween, Vec2, Vec3 } from 'cc';
import { DIRECTION_ENUM, EAT_STATUS_ENUM, GAME_MODE_ENUM, PHY_GROUP } from '../../Enums';
import { createUINode } from '../utils';
import DataManager from '../Runtime/DataManager';
import { StoneManager } from '../Property/StoneManager';
import { BrickManager } from '../Property/BrickManager';
import { HeavyManager } from '../Property/HeavyManager';
import { JellyManager } from '../Property/JellyManager';
import { BombManager } from '../Property/BombManager';
import { ExplosionTimingManager } from '../Property/ExplosionTimingManager';
const { ccclass } = _decorator;

@ccclass('AttackManager')
// 攻击脚本
export class AttackManager extends Component {
    jellyAttackList: Array<JellyManager> = []
    // 发射道具
    launchAttack(type: EAT_STATUS_ENUM, direction: DIRECTION_ENUM) {
        // 隐形子弹
        if (type == EAT_STATUS_ENUM.EMPTY) {
            const nullNode = createUINode("nullAttack")
            nullNode.setParent(this.node)
            nullNode.setPosition(0, 20)
            const rigidBody2D = nullNode.addComponent(RigidBody2D)
            rigidBody2D.gravityScale = 0
            rigidBody2D.type = ERigidBody2DType.Kinematic
            rigidBody2D.fixedRotation = true
            const boxCollider2D = nullNode.addComponent(BoxCollider2D)
            boxCollider2D.size = new Size(6, 15)
            boxCollider2D.sensor = true
            rigidBody2D.enabledContactListener = true
            let x = 0, y = 0
            let time = .6
            switch (direction) {
                case DIRECTION_ENUM.TOP:
                    y = 100;
                    time = 1.4
                    break;
                case DIRECTION_ENUM.BOTTOM:
                    y = -120;
                    break;
                case DIRECTION_ENUM.LEFT:
                    x = -180
                    break;
                case DIRECTION_ENUM.RIGHT:
                    x = 180
                    break;
            }
            tween(nullNode)
                .by(time, { position: new Vec3(x, y, 0) })
                .call(() => {
                    if (nullNode && nullNode.isValid) {
                        nullNode.destroy();
                    }
                })
                .start()
        }
        // 石头
        if (type == EAT_STATUS_ENUM.STONE) {
            let name="stoneAttack"
            if(DataManager.Instance.gameMode==GAME_MODE_ENUM.ONLINE){
                name='stoneAttack'+DataManager.Instance.eatName.replace("stone",'')
            }
            const stoneNode = createUINode(name)
            stoneNode.setParent(this.node.parent.parent)
            const stoneManager = stoneNode.addComponent(StoneManager)
            stoneManager.init()
            this.handleAttack(stoneManager, stoneNode, direction)
        }
        // 大石头
        if (type == EAT_STATUS_ENUM.HEAVY) {
            let name="heavyAttack"
            if(DataManager.Instance.gameMode==GAME_MODE_ENUM.ONLINE){
                name='heavyAttack'+DataManager.Instance.eatName.replace("heavy",'')
            }
            const heavyNode = createUINode(name)
            heavyNode.setParent(this.node.parent.parent)
            const heavyManager = heavyNode.addComponent(HeavyManager)
            heavyManager.init()
            this.handleAttack(heavyManager, heavyNode, direction, 2)
        }
        // 砖块
        if (type == EAT_STATUS_ENUM.BRICK) {
            let name="brickAttack"
            if(DataManager.Instance.gameMode==GAME_MODE_ENUM.ONLINE){
                name='brickAttack'+DataManager.Instance.eatName.replace("brick",'')
            }
            const brickNode = createUINode(name)
            brickNode.setParent(this.node.parent.parent)
            const brickManager = brickNode.addComponent(BrickManager)
            brickManager.init()
            this.handleAttack(brickManager, brickNode, direction)
        }
        // 果冻
        if (type == EAT_STATUS_ENUM.JELLY) {
            let name="jellyAttack"
            if(DataManager.Instance.gameMode==GAME_MODE_ENUM.ONLINE){
                name='jellyAttack'+DataManager.Instance.eatName.replace("jelly",'')
            }
            const jellyNode = createUINode(name)
            jellyNode.setParent(this.node.parent.parent)
            const jellyManager = jellyNode.addComponent(JellyManager)
            jellyManager.init()
            this.jellyAttackList.push(jellyManager)
            this.handleAttack(jellyManager, jellyNode, direction)
        }
        // 吐炸弹
        if (type == EAT_STATUS_ENUM.BOMB) {
            const explosionTimingManager = this.node.getComponent(ExplosionTimingManager)
            explosionTimingManager.destroy()
            const bombLabel = this.node.getChildByName('bombLabel')
            bombLabel.destroy()
            let name="bombAttack"
            if(DataManager.Instance.gameMode==GAME_MODE_ENUM.ONLINE){
                name='bombAttack'+DataManager.Instance.eatName.replace("bomb",'')
            }
            const bombNode = createUINode(name)
            bombNode.setParent(this.node.parent.parent)
            const bombManager = bombNode.addComponent(BombManager)
            bombManager.init()
            this.handleAttack(bombManager, bombNode, direction)
        }
    }
    handleAttack(manager: JellyManager | StoneManager | BombManager | BrickManager, node: Node, direction: DIRECTION_ENUM, linearDamping: number = 0) {
        let { x, y } = this.node.parent.getPosition()
        const rigidbody = manager.getComponent(RigidBody2D)
        rigidbody.type = ERigidBody2DType.Dynamic
        rigidbody.group = PHY_GROUP.DEFAULT
        rigidbody.gravityScale = 0
        rigidbody.fixedRotation = true
        const boxCollider2D = manager.getComponent(BoxCollider2D)
        let width = DataManager.Instance.width
        let position = DataManager.Instance.position
        let forceX = 0, forceY = 0
        let positionX = 0, positionY = -30
        let size_x = width + 50, size_y = position -10
        let user_direction=null
        switch (direction) {
            case DIRECTION_ENUM.TOP:
                forceY = 0.02
                positionY = -120 - (45 / 2)
                size_x = 10
                user_direction=DIRECTION_ENUM.BOTTOM
                break;
            case DIRECTION_ENUM.BOTTOM:
                forceY = -0.02
                positionY = 95
                size_x = 10
                size_y=position-40
                user_direction=DIRECTION_ENUM.TOP
                break;
            case DIRECTION_ENUM.LEFT:
                forceX = -0.02
                positionX = 155 + (50 / 2)
                size_y = 10
                user_direction=DIRECTION_ENUM.RIGHT
                break;
            case DIRECTION_ENUM.RIGHT:
                forceX = 0.02
                positionX = -160 - (50 / 2)
                size_y = 10
                user_direction=DIRECTION_ENUM.LEFT
                break;
        }
        rigidbody.linearDamping = linearDamping
        boxCollider2D.size = new Size(size_x, size_y)
        boxCollider2D.sensor = true
        node.setPosition(x - positionX, y - positionY)
        let force = new Vec2(forceX, forceY)
        rigidbody.applyLinearImpulseToCenter(force, true)
        manager.start_x=node.getPosition().x
        manager.start_y=node.getPosition().y
        manager.user_direction=user_direction
        manager.parent_node=this.node.parent
        manager.is_running = true
        if(DataManager.Instance.gameMode==GAME_MODE_ENUM.ONLINE){
            DataManager.Instance.eatName=""
            let name=node.name.replace("Attack",'')
            let index=DataManager.Instance.async_scence.findIndex(item=>item.node==name)
            if(index!=-1){
                DataManager.Instance.async_scence[index]={ type: 'property', node: name, player: manager }
            }
        }
      
    }
}


